package com.taomeishu.mailcrazy.checkers.sina;

import com.taomeishu.mailcrazy.CheckResult;
import com.taomeishu.mailcrazy.Mail;
import com.taomeishu.mailcrazy.http.ClientResponse;
import com.taomeishu.mailcrazy.parser.ResultParseException;
import com.taomeishu.mailcrazy.parser.ResultParser;

public class SinaResultParser implements ResultParser {

	@Override
	public CheckResult parse(ClientResponse clientResponse, String username, String domainName) throws ResultParseException {
		String s = new String(clientResponse.getData());
		CheckResult result = new CheckResult(true);
		Mail mail = new Mail(username, domainName);
		result.setMail(mail);
		// cn yes com yes
		String[] parts = s.split(" ");
		if (parts.length == 1) {
			if (s.trim().equals("yes")) {
				result.addExistMail(new Mail(username, "sina.com"));
			} else {
				result.addNotExistMail(new Mail(username, "sina.com"));
			}
		} else {
			proccess(parts[0], parts[1], username, result);
			proccess(parts[2], parts[3], username, result);
		}
		return result;
	}
	
	private void proccess(String name, String s, String username, CheckResult result) {
		if ("cn".equals(name)) {
			boolean cnExist = "yes".equals(s);
			if (cnExist) {
				result.addExistMail(new Mail(username, "sina.cn"));
			} else {
				result.addNotExistMail(new Mail(username, "sina.cn"));
			}
		} else if ("com".equals(name)) {
			boolean cnExist = "yes".equals(s);
			if (cnExist) {
				result.addExistMail(new Mail(username, "sina.com"));
			} else {
				result.addNotExistMail(new Mail(username, "sina.com"));
			}
		}
	}

}
